package com.forlove.app.qselect.answer.presenter;

import android.text.TextUtils;

import com.forlove.app.ForloveApplication;
import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.manager.FriendManager;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.answer.entity.AnswerEntity;
import com.forlove.app.qselect.answer.entity.AnswerResultEntity;
import com.forlove.app.qselect.answer.entity.MsgInfoEntity;
import com.forlove.app.qselect.answer.entity.SubjectsEntity;
import com.forlove.app.qselect.answer.service.IAnswerSubjectService;
import com.forlove.app.qselect.answer.service.impl.AnswerSubjectServiceImpl;
import com.forlove.app.qselect.answer.view.AnswerSubjectView;
import com.forlove.app.qselect.friend.entity.FriendEntity;
import com.forlove.app.qselect.friend.entity.FriendListEntity;
import com.forlove.app.qselect.friend.entity.MineFriendEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yjl on 2017/11/1.
 */

public class AnswerSubjectPresenter extends IBasePresenter {
    private AnswerSubjectView answerSubjectView;
    private IAnswerSubjectService answerSubjectService;

    public AnswerSubjectPresenter(AnswerSubjectView answerSubjectView) {
        this.answerSubjectView = answerSubjectView;
        if (answerSubjectService == null) {
            answerSubjectService = new AnswerSubjectServiceImpl();
            setService(answerSubjectService);
        }
    }


    public synchronized void loadQuestionData() {
        Map<String, String> param = new HashMap<>();
        answerSubjectService.loadData(ServerManager.ACTION_GET_SUBJECT, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "获取题目 ：" + result);
                AnswerEntity answerEntity = ParseHelper.getModel(result, "data", AnswerEntity.class);
                answerSubjectView.updateSubjectsView(answerEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                LogUtil.e("yjl", "获取题目 onFail" + result);
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    answerSubjectView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else if (answerSubjectView != null) {
                    answerSubjectView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "请稍后重试!");
                }
            }
        });
    }

    /**
     * 答题
     *
     * @param param
     */
    public synchronized void loadAnswerSubject(Map param) {
        answerSubjectService.loadData(ServerManager.ACTION_ANSWER_SUBJECT, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "答题：" + result);
//                long time = ParseHelper.getLong(result, "data.expire");
//                String scores = ParseHelper.getString(result, "data.scores");
                AnswerResultEntity answerResultEntity = ParseHelper.getModel(result, "data", AnswerResultEntity.class);
//                if (time > 0) {
                answerSubjectView.updateAnswerSubject(NetworkManager.ResultCode.RESULT_SUCCESS, answerResultEntity);
//                } else {
//                    answerSubjectView.updateAnswerSubject(NetworkManager.ResultCode.RESULT_SUCCESS, time, scores);
//                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    answerSubjectView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else if (answerSubjectView != null) {
                    answerSubjectView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "请稍后重试!");
                }
            }
        });
    }


    /**
     * 获取答题德详情页
     */
    public synchronized void loadDetailInfoData(int recordId, int type) {
        Map param = new HashMap();
        param.put("recordId", recordId);
        param.put("type", type);
        answerSubjectService.loadData(ServerManager.ACTION_USER_ANSWER_DETAIL, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "答题记录详情:" + result);
                MsgInfoEntity msgInfoEntity = ParseHelper.getModel(result, "data.msgInfo", MsgInfoEntity.class);
                SubjectsEntity subjectsEntity = ParseHelper.getModel(result, "data.subject", SubjectsEntity.class);
                if (answerSubjectView != null) {
                    answerSubjectView.updateDetailView(msgInfoEntity, subjectsEntity);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (answerSubjectView != null) {
                    if (!TextUtils.isEmpty(commonEntity.message)) {
                        answerSubjectView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    } else {
                        answerSubjectView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "请稍后重试!");
                    }
                }
            }
        });
    }

    /**
     * 被圈详情信息
     *
     * @param recordId
     */
    public synchronized void loadDetailLockData(int recordId) {
        Map param = new HashMap();
        param.put("recordId", recordId);
        answerSubjectService.loadData(ServerManager.ACTION_ANSWER_DETAIL_LOCK, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "锁题:" + result);
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (answerSubjectView != null) {
                    answerSubjectView.updateLockDetailView(NetworkManager.ResultCode.RESULT_SUCCESS, commonEntity.message);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (answerSubjectView != null) {
                    if (!TextUtils.isEmpty(commonEntity.message)) {
                        answerSubjectView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    } else {
                        answerSubjectView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "请稍后重试!");
                    }
                }
            }
        });

    }

    /**
     * 获取好友列表
     */
    public synchronized void loadFriendListData() {
        answerSubjectService.loadData(ServerManager.ACTION_FRIEND_LIST, null, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "好友列表:" + result);
                List<MineFriendEntity> friendEntities = new ArrayList<>();
                friendEntities = ParseHelper.getModelList(result, "data.friends", FriendEntity.class);
                List<MineFriendEntity> contacts = ParseHelper.getModelList(result, "data.concats", FriendEntity.class);
                FriendListEntity friendListEntity = ParseHelper.getModel(result, "data", FriendListEntity.class);
                if (friendListEntity != null && friendListEntity.friendEntities.size() > 0) {
                    FriendManager.saveFriendList(ForloveApplication.getInstance(), result);
                }
                answerSubjectView.updateFriendListView(FriendManager.mergeFriends(friendEntities, contacts, -1));
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    answerSubjectView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    answerSubjectView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }

    public synchronized void loadCondidateFriends(List<MineFriendEntity> mineFriendEntities) {
        if (mineFriendEntities == null) {
            return;
        }
        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < mineFriendEntities.size(); i++) {
            MineFriendEntity mineFriendEntity = mineFriendEntities.get(i);
            if (StringUtils.isNotEmpty(mineFriendEntity.mobile)) {
                stringBuilder.append(mineFriendEntity.mobile);
            } else {
                stringBuilder.append(mineFriendEntity.contactsNumber);
            }
            if (i < mineFriendEntities.size() - 1) {
                stringBuilder.append(",");
            }
        }
        Map param = new HashMap();
        param.put("list", stringBuilder.toString());
        answerSubjectService.loadData(ServerManager.ACTION_ADD_FRIENDS, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                answerSubjectView.updateSetCandidateView(commonEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    answerSubjectView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    answerSubjectView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }

    public void requestCandidateList() {//获取后台返回的候选人列表
        answerSubjectService.loadData(ServerManager.ACTION_CANDIDATE_LIST, new HashMap(), new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "候选人列表" + result.toString());

                List<FriendEntity> candidateEntity = ParseHelper.getModelList(result, "data.concats", FriendEntity.class);

//                answerSubjectView.updateCandidate(candidateEntity);

            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    answerSubjectView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    answerSubjectView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }

}
